//index.js
//获取应用实例
const app = getApp()
import {
	request
} from '../../utils/api.js'
import {
	meun,
	navs0,
	navs1,
	navs2,
	navs3,
	navs4
} from '../../utils/json.js'
import {
	isopenblue,
	removeNull,
	toauth
} from '../../utils/util.js'
const plugin = requirePlugin("myPlugin");
Page({
	data: {
		navs: [],
		key: null,
		userinfo: null,
		lockinfo: {}
	},
	jump(e) {
		let url = e.currentTarget.dataset.url;
		if (!app.globalData.auth) {
			url = '/pages/login/auth/auth'
		}
		wx.navigateTo({
			url
		});
	},
	//事件处理函数
	tonavs: function(event) {
		toauth();
		const text = event.currentTarget.dataset.text;
		let url = '';
		switch (text) {
			case '申请':
				url = '/pages/menu0/menu0';
				break;
			case '审批':
				url = '/pages/menu1/menu1';
				break;
			case '授权':
				url = '/pages/menu2/menu2';
				break;
			case '申请记录':
				url = '/pages/menu3/menu3';
				break;
			case '授权记录':
				url = '/pages/menu4/menu4';
				break;
			case '历史密码':
				url = '/pages/menu5/menu5';
				break;
			case '安装工':
				url = '/pages/menu6/menu6';
				break;
		}
		wx.navigateTo({
			url: url
		})
	},

	lock() {
		toauth();
		const lockinfo=this.data.lockinfo;
		if (!lockinfo) {
			wx.navigateTo({
				url: '/pages/lock/lock?user=' + JSON.stringify(this.data.userinfo)
			})
		} else {
			this.unlock(lockinfo);
		}
	},
	unlock(lockinfo) {
		isopenblue((err) => {
			if (err) {
				wx.showModal({
					content: "蓝牙未开启"
				})
				return;
			} else {
				request({
					url: '/api/wxapi/lan_openlock',
					header: {
						token: wx.getStorageSync('token')
					},
					data: {
						lock_id: lockinfo.lock.id
					}
				}).then(res => {
					if (res.data.code == 1) {
						const lock_data = res.data.lock_data;
						plugin.controlLock(3, lock_data, res => {
							console.log(res);
							app.globalData.info=null;
						})
					} else {
						wx.showModal({
							content: res.data.msg
						})
					}
				});
			}
		});
	},
	getkey() {
		if (!app.globalData.auth) {
			wx.navigateTo({
				url: '/pages/login/auth/auth',
			})
			return;
		}
		const lockinfo =this.data.lockinfo;
		if (lockinfo) {
			request({
				url: '/api/wxapi/get_pwd',
				header: {
					token: wx.getStorageSync('token')
				},
				data: {
					lockid: lockinfo.lock.id
				}
			}).then(res => {
				if (res.data.code == 1) {
					this.setData({
						key: res.data.lockpwd
					})
					wx.setStorageSync('info', null);
				} else {
					wx.showModal({
						content: res.data.msg
					})
				}

			})
		} else {
			wx.navigateTo({
				url: '/pages/lock/lock?user=' + JSON.stringify(this.data.userinfo)
			})
		}
	},
	getlockinfo() {
		const lockinfo =app.globalData.info;
		this.setData({
			lockinfo
		})
	},
	onShow: function() {
		
		this.getlockinfo();
		this.getmenu();
	},
	getmenu(){
		
    meun((res) => {
			const type = res.data.user_info.admin_type;
			const data = removeNull(res.data.user_info);
			wx.setStorageSync('userinfo',data)
			this.setData({
				userinfo: data
			})
			let navs = [];
			switch (type) {
				case 0:
					navs = navs0;
					break;
				case 1:
					navs = navs1;
					break;
				case 2:
					navs = navs2;
					break;
				case 4:
					navs = navs4;
					break;
				case 3:
					navs = navs3;
					break;
			}
			wx.setStorageSync('navs', navs)
			this.setData({
				navs
			})
		});
	},
	onLoad: function() {
		console.log('lock')
	}
})
